#include "picscale.h"

picscale::picscale()
{
    swr = NULL;
}

int picscale::init(uint32_t src_height, uint32_t src_wight, int src_fmt, uint32_t dst_height, uint32_t dst_wight, int dst_fmt)
{
    this->src_height = src_height;
    this->src_fmt = (AVPixelFormat)src_fmt;
    this->src_wight = src_height;

    this->dst_fmt =(AVPixelFormat)dst_fmt;
    this->dst_height = dst_height;
    this->dst_wight = dst_wight;

//    swr_init()
    swr = sws_getContext(src_wight,src_height,(AVPixelFormat)src_fmt,dst_wight,dst_height,(AVPixelFormat)dst_fmt,this->algo_type,NULL,NULL,NULL);
    if (!swr) {
        std::cout<<"error"<<std::endl;
        return -1;
    }
    return 0;
}

int picscale::sale(const Frame *src_frame, VideoFrame *dst_frame)
{

    int ret=0;
    if(src_frame->width != src_wight
            || src_frame->height != src_height
            || src_frame->format != src_fmt
            || dst_frame->width != dst_wight
            || dst_frame->height != dst_height
            || dst_frame->format != dst_fmt
            || !swr){

           sws_freeContext(swr);
           swr = NULL;
           ret = init((uint32_t)(src_frame->height),(uint32_t)(src_frame->width),src_frame->format,(uint32_t)dst_height,(uint32_t)dst_wight,dst_fmt);
           if(ret<0){
               return -1;
           }
    }
//    swr_scale
    ret = sws_scale(swr,
                    (const uint8_t**)src_frame->frame->data,
                    src_frame->frame->linesize,
                    0,
                    src_frame->height,
                    dst_frame->data,dst_frame->linesize);
    if(ret>0){
        return 0;
    }else{
        return -1;
    }
}


//RET_CODE Scale3(const Frame *src_frame, VideoFrame *dst_frame) {
//    if(src_frame->width != src_width_
//            || src_frame->height != src_height_
//            || src_frame->format != src_pix_fmt_
//            || dst_frame->width != dst_width_
//            || dst_frame->height != dst_height_
//            || dst_frame->format != dst_pix_fmt_
//            || !sws_ctx_) {
//        DeInit();
//        RET_CODE ret = Init(src_frame->width, src_frame->height, src_frame->format,
//                            dst_frame->width, dst_frame->height, dst_frame->format,
//                            en_alogrithm_);
//        if(ret != RET_OK) {
//            LogError("Init failed");
//            return ret;
//        }
//    }
//    int dst_slice_h = sws_scale(sws_ctx_,
//                                (const uint8_t **)src_frame->frame->data,
//                                src_frame->frame->linesize,
//                                0,  // 起始位置
//                                src_frame->height, //处理多少行
//                                dst_frame->data,
//                                dst_frame->linesize);
//    if(dst_slice_h>0)
//        return RET_OK;
//    else
//        return RET_FAIL;
//}
